const app = getApp()
import { index } from '../../api/design'
import { getQueryObject } from '../../utils/util'

Page({
  data: {
    theme: '',
    title: '',
    design: [],
    loading: true
  },
  onLoad (options) {
    this.init()
    this.sceneHandle(options.scene)
    this.shareHandle(options)
  },
  onPullDownRefresh () { /* 监听用户下拉动作*/ },
  onReachBottom () { /* 页面上拉触底事件的处理函数*/ },
  onShareAppMessage (event) {
    return {
      path: '/pages/index/index'
    }
  },

  // ===================methon================

  init () {
    const that = this
    that.setData({ loading: true })
    index('index').then(res => {
      const { data } = res
      const skin = data[0].data
      
      app.global.theme = data[0].data.app_skin
      app.global.title = data[0].data.app_name
      app.global.color = data[0].data.navigation_bar_text_style
      app.global.tabbar = data[1].data  // tabbar 组件

      wx.setNavigationBarColor({
        backgroundColor: '#ffffff',
        frontColor: skin.navigation_bar_text_style
      })
      
      that.setData({
        theme: data[0].data.app_skin,
        title: data[0].data.app_name,
        color: data[0].data.navigation_bar_text_style,
        design: data
      })
      // wx.setStorageSync('theme', skin.app_skin)
      // wx.setStorageSync('navigation-bar-title', skin.app_name)
      // wx.setStorageSync('color', skin.navigation_bar_text_style)
    })
      .finally(() => {
        this.setData({ loading: false })
      })
  },

  sceneHandle (scene) {
    // console.log('Scene:', scene);
    // console.log('Scene decodeURIComponent:', decodeURIComponent(scene));
    scene = decodeURIComponent(scene)

    if (scene != undefined && scene != 'undefined') {
      const params = getQueryObject(scene)
      if (params.u) {
        wx.setStorage({
          key: 'invite',
          data: params.u
        })
      }
      switch (params.t) {
        // t=0 是商品
        case '0':
          wx.navigateTo({
            url: '/pages/goods/detail/index?id=' + params.id
          })
          break
        // t=99 是核销码
        case '99':
          wx.navigateTo({
            url: '/pages/order/fetch/index?id=' + params.id
          })
          break
      }
    }
  },

  shareHandle (options) {
    // console.log('Share:', options)
    if (options != undefined && options != 'undefined') {
      if (options.share == 'goods') {
        wx.navigateTo({
          url: '/pages/goods/detail/index?id=' + options.id
        })
      }
      if (options.share == 'article') {
        wx.navigateTo({
          url: '/pages/article/detail/index?id=' + options.id
        })
      }
    }
  }
})